ViewPagerSwipe
Merhaba;
Projeme dahil etmek istediğim bir ViewPager uygulaması var ancak image butonunu ortalayamıyorum yardımcı olabilirmisiniz.
Paylastığım resimde ne istediğim daha açık butonu ortalamak amacım.
Kodlar;
public class MainActivity extends Activity
{
// Declare Variables
ViewPager viewPager;
PagerAdapter adapter;
int[] flag;
LinearLayout llDots;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Get the view from viewpager_main.xml
setContentView(R.layout.viewpager_main);
flag = new int[] { R.drawable.yuvarlak, R.drawable.yuvarlak,
R.drawable.yuvarlak, R.drawable.yuvarlak,
R.drawable.yuvarlak, R.drawable.yuvarlak, R.drawable.yuvarlak,
R.drawable.yuvarlak, R.drawable.yuvarlak, R.drawable.yuvarlak };
llDots=(LinearLayout) findViewById(R.id.llDots);
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new ViewPagerAdapter(this, flag);
viewPager.setAdapter(adapter);
for (int i = 0; i < adapter.getCount(); i++)
{
ImageButton imgDot = new ImageButton(this);
imgDot.setTag(i);
imgDot.setImageResource(R.drawable.dot_selector);
imgDot.setBackgroundResource(0);
imgDot.setPadding(5, 5, 5, 5);
LayoutParams params = new LayoutParams(20, 20);
imgDot.setLayoutParams(params);
if(i == 0)
imgDot.setSelected(true);
llDots.addView(imgDot);
}
viewPager.setOnPageChangeListener(new OnPageChangeListener()
{
@Override
public void onPageSelected(int pos)
{
Log.e("", "Page Selected is ===> " + pos);
for (int i = 0; i < adapter.getCount(); i++)
{
if(i != pos)
{
((ImageView)llDots.findViewWithTag(i)).setSelected(false);
}
}
((ImageView)llDots.findViewWithTag(pos)).setSelected(true);
}
@Override
public void onPageScrolled(int pos, float arg1, int arg2)
{
}
@Override
public void onPageScrollStateChanged(int arg0)
{
}
});
//int margin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20*2, getResources().getDisplayMetrics());
//viewPager.setPageMargin(-20);
}
}
-------------------------------------------------------------------------------
public class ViewPagerAdapter extends PagerAdapter
{
// Declare Variables
Context context;
int[] flag;
LayoutInflater inflater;
public ViewPagerAdapter(Context context, int[] flag)
{
this.context = context;
this.flag = flag;
}
@Override
public int getCount()
{
return flag.length;
}
@Override
public boolean isViewFromObject(View view, Object object)
{
return view == ((RelativeLayout) object);
}
public float getPageWidth(int position)
{
return 0.7f;
}
@Override
public Object instantiateItem(ViewGroup container, int position)
{
ImageView imgflag;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.viewpager_item, container,false);
// Locate the ImageView in viewpager_item.xml
imgflag = (ImageView) itemView.findViewById(R.id.flag);
// Capture position and set to the ImageView
imgflag.setImageResource(flag[position]);
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}